package cn.hhy.hrm.Exception;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class ValidUtils {

    //手机号验证规则
    private static final String PHONE_REGEX = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9]))\\d{8}$";
    //邮箱的校验规则
    private static final String EMAIL_REGEX = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    /*
    非空断言
     */
    public static void assertNotNull(Object o,ErrorCode code){
        if(o==null){
            throw new BasicException(code);

        }
    }
    public static void assertNull(Object o,ErrorCode code){
        if(o!=null){
            throw new BasicException(code);

        }
    }
    public static void assertNotEmpty(String s,ErrorCode code){
        if(s==null || "".equals(s.trim())){
            throw new BasicException(code);

        }
    }
    //断言验证手机号
    public static void assertPhone(String phone,ErrorCode code){
        //格式判断
        Pattern p = Pattern.compile(PHONE_REGEX);
        Matcher m = p.matcher(phone);
        if(!m.matches()){
            throw new BasicException(code);
        }
    }

    public static void assertPhone(String phone){
        assertPhone(phone,ErrorCode.CODE_500_PARAMETER_PHONE);
    }


    //断言验证邮箱
    public static void assertEmail(String email,ErrorCode code){
        //格式判断
        Pattern p = Pattern.compile(EMAIL_REGEX);
        Matcher m = p.matcher(email);
        if(!m.matches()){
            throw new BasicException(code);
        }
    }
    public static void assertEmail(String email){
        assertEmail(email,ErrorCode.CODE_500_PARAMETER_EMAIL);
    }

}
